// 用于字典的组件： 页面上的单选框、多选的字典
<template>
<!-- tagType： 用于不同类型的字典的展示 -->
    <a-radio-group v-if="tagType=='radio'">
        <a-radio v-for="(item,key) in dictOptions" :key="key" @change="handleInput" :value="getValueString" :disabled="disabled"></a-radio>
    </a-radio-group>

   <!-- select下拉 -->
   <a-select v-else-if="tagType=='select'" :getPopupContainer = "getPopupContainer" :placeholder="placeholder" :disabled="disabled" :value="getValueSting" @change="handleInput">
    <a-select-option :value="undefined">请选择</a-select-option>
    <a-select-option v-for="(item, key) in dictOptions" :key="key" :value="item.value">
      <span style="display: inline-block;width: 100%" :title=" item.text || item.label ">
        {{ item.text || item.label }}
      </span>
    </a-select-option>
  </a-select>
</template>
<script>
import {getDictItemsFromCache,getDictItems} from './api/dict-api'
export default {
    name: 'JDictSelectTag',
    props: {
        dictCode: String,
        placeholder: String,
        disabled: Boolean,
        value: [String,Number],
        type: String,
        getPopupContainer: {
            type: Function,
            default: (node)=> node.parentNode
        }
    },
    computed: {
        getValueString() {
            return this.value != null?this.value.toString():undefined;
        }
    },
    data() {
        return {
            tagType: "",
            dictOptions: []
        }
    },
    watch: {
        dictCode: {
            immediate: true,
            handler() {
                this.initDictData();
            }
        }
    },
    methods: {
        initDictData() {
            if(getDictItemsFromCache(this.dictCode)) {
                this.dictOptions = getDictItemsFromCache(this.dictCode);
                return
            }
            // 请求接口获取字典数据
            getDictItems(this.dictCode).then(res=>{
                if(res.success) {
                    this.dictOptions = res.result;
                }
            })
        },
        handleInput() {

        }
    }
}
</script>